package soccerscope.view;

import javax.swing.JDialog;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import soccerscope.db.DatabaseConfig;
import soccerscope.db.DatabaseReader;
import soccerscope.db.RobocupDatabase;

import javax.swing.JCheckBox;

/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author Linh Phong Le
 * @version 1.0
 */
public class DatabaseConfigDialog
    extends JDialog {

  /**
   * 
   */
  private static final long serialVersionUID = -3286487016798530614L;
  private DatabaseConfig    dbConfig         = DatabaseConfig.getInstance();

  public DatabaseConfigDialog() {

    this.dbConfig.load();

    try {
      this.jbInit();

      this.configInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void configInit() {

    this.jHostTextField.setText(this.dbConfig.getSqlHost());
    this.jDatabaseTextField.setText(this.dbConfig.getSqlDB());
    this.jUserTextField.setText(this.dbConfig.getSqlUser());
    this.jPasswordTextField.setText(this.dbConfig.getSqlPassword());
    this.jDetectPlayingCheckBox.setSelected(this.dbConfig.isDetectPlaying());
    this.jDetectTeamsCheckBox.setSelected(this.dbConfig.isDetectTeams());
  }

  public static void main(String[] args) {

    DatabaseConfigDialog databaseconfigdialog = new DatabaseConfigDialog();
  }

  private void jbInit() throws Exception {

    this.getContentPane().setLayout(this.borderLayout1);
    this.jCancelButton.setText("Cancel");
    this.jCancelButton
        .addActionListener(new DatabaseConfigDialog_jCancelButton_actionAdapter(
            this));
    this.jPanel1.setLayout(null);
    this.jLabel1.setText("Host name");
    this.jLabel1.setBounds(new Rectangle(29, 41, 67, 14));
    this.jHostTextField.setText("");
    this.jHostTextField.setBounds(new Rectangle(132, 38, 192, 20));
    this.jLabel3.setText("Database name");
    this.jLabel3.setBounds(new Rectangle(29, 90, 97, 14));
    this.jDatabaseTextField.setText("");
    this.jDatabaseTextField.setBounds(new Rectangle(132, 87, 168, 20));
    this.jLabel4.setText("User name");
    this.jLabel4.setBounds(new Rectangle(29, 126, 69, 14));
    this.jUserTextField.setText("");
    this.jUserTextField.setBounds(new Rectangle(132, 124, 161, 20));
    this.jLabel5.setText("Password");
    this.jLabel5.setBounds(new Rectangle(29, 175, 62, 14));
    this.jPasswordTextField.setText("");
    this.jPasswordTextField.setBounds(new Rectangle(132, 173, 164, 20));
    this.setTitle("Game settings");
    this.jClearButton
        .addActionListener(new DatabaseConfigDialog_jClearButton_actionAdapter(
            this));
    this.jSaveButton
        .addActionListener(new DatabaseConfigDialog_jSaveButton_actionAdapter(
            this));
    this.jDetectPlayingCheckBox.setText("Record only after game starts");
    this.jDetectPlayingCheckBox.setBounds(new Rectangle(27, 248, 249, 22));
    this.jDetectTeamsCheckBox
        .setText("Record only after both teams are loaded");
    this.jDetectTeamsCheckBox.setBounds(new Rectangle(28, 213, 259, 22));
    this.getContentPane().add(this.jPanel1, java.awt.BorderLayout.CENTER);
    this.jPanel1.add(this.jLabel1);
    this.jPanel1.add(this.jLabel3);
    this.jPanel1.add(this.jLabel4);
    this.jPanel1.add(this.jHostTextField);
    this.jPanel1.add(this.jUserTextField);
    this.jPanel1.add(this.jLabel5);
    this.jPanel1.add(this.jPasswordTextField);
    this.jPanel1.add(this.jDatabaseTextField);
    this.jPanel1.add(this.jDetectTeamsCheckBox);
    this.jPanel1.add(this.jDetectPlayingCheckBox);
    this.jSaveButton.setText("Save");
    this.getContentPane().add(this.jPanel2, java.awt.BorderLayout.SOUTH);
    this.jClearButton.setText("Clear");
    this.jPanel2.add(this.jClearButton);
    this.jPanel2.add(this.jSaveButton);
    this.jPanel2.add(this.jCancelButton);
    this.setBounds(150, 150, 400, 380);
  }

  BorderLayout borderLayout1          = new BorderLayout();
  JPanel       jPanel1                = new JPanel();
  JPanel       jPanel2                = new JPanel();
  JButton      jSaveButton            = new JButton();
  JButton      jClearButton           = new JButton();
  JButton      jCancelButton          = new JButton();
  JLabel       jLabel1                = new JLabel();
  JTextField   jHostTextField         = new JTextField();
  JLabel       jLabel3                = new JLabel();
  JTextField   jDatabaseTextField     = new JTextField();
  JLabel       jLabel4                = new JLabel();
  JTextField   jUserTextField         = new JTextField();
  JLabel       jLabel5                = new JLabel();
  JTextField   jPasswordTextField     = new JTextField();
  JCheckBox    jDetectPlayingCheckBox = new JCheckBox();
  JCheckBox    jDetectTeamsCheckBox   = new JCheckBox();

  public void jClearButton_actionPerformed(ActionEvent e) {

    this.jHostTextField.setText("");
    this.jDatabaseTextField.setText("");
    this.jUserTextField.setText("");
    this.jPasswordTextField.setText("");
    this.jDetectPlayingCheckBox.setSelected(this.dbConfig.isDetectPlaying());
    this.jDetectTeamsCheckBox.setSelected(this.dbConfig.isDetectTeams());
  }

  public void jSaveButton_actionPerformed(ActionEvent e) {

    DatabaseConfig dbConfig = DatabaseConfig.getInstance();
    dbConfig.setSqlHost(this.jHostTextField.getText());
    dbConfig.setSqlDB(this.jDatabaseTextField.getText());
    dbConfig.setSqlUser(this.jUserTextField.getText());
    dbConfig.setSqlPassword(this.jPasswordTextField.getText());

    dbConfig
        .setDetectPlaying(this.jDetectPlayingCheckBox.isSelected() ? "true" : "false");
    dbConfig
        .setDetectTeams(this.jDetectTeamsCheckBox.isSelected() ? "true" : "false");
    dbConfig.store();

    // aktualisiert die DatabaseReader-Instanz
    DatabaseReader dbReader = DatabaseReader.getInstance();
    if (dbReader != null) {
      DatabaseReader.restartInstance();
    }

    RobocupDatabase rcDatabase = RobocupDatabase.getInstance();
    if (rcDatabase != null) {
      RobocupDatabase.restartInstance();
    }

    this.dispose();
  }

  public void jCancelButton_actionPerformed(ActionEvent e) {

    this.dispose();
  }
}

class DatabaseConfigDialog_jCancelButton_actionAdapter
    implements ActionListener {

  private DatabaseConfigDialog adaptee;

  DatabaseConfigDialog_jCancelButton_actionAdapter(
      DatabaseConfigDialog adaptee) {

    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {

    this.adaptee.jCancelButton_actionPerformed(e);
  }
}

class DatabaseConfigDialog_jSaveButton_actionAdapter
    implements ActionListener {

  private DatabaseConfigDialog adaptee;

  DatabaseConfigDialog_jSaveButton_actionAdapter(
      DatabaseConfigDialog adaptee) {

    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {

    this.adaptee.jSaveButton_actionPerformed(e);
  }
}

class DatabaseConfigDialog_jClearButton_actionAdapter
    implements ActionListener {

  private DatabaseConfigDialog adaptee;

  DatabaseConfigDialog_jClearButton_actionAdapter(
      DatabaseConfigDialog adaptee) {

    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {

    this.adaptee.jClearButton_actionPerformed(e);
  }
}
